import { fileURLToPath, URL } from 'node:url'
import path from 'path'
import { defineConfig, loadEnv, type ConfigEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
// import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import IconsResolver from 'unplugin-icons/resolver'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

// https://vite.dev/config/
export default defineConfig(({ mode }: ConfigEnv) => {
  const env = loadEnv(mode, process.cwd(), '')
  return {
    plugins: [
      vue(),
      vueJsx(),
      // vueDevTools(),
      AutoImport({
        dts: true,
        resolvers: [
          ElementPlusResolver({
            importStyle: false,
            // directives: true,
            // version: '2.1.5',
          }),
          IconsResolver({
            prefix: 'Icon',
          }),
        ],
      }),
      Components({
        resolvers: [
          ElementPlusResolver({
            importStyle: false,
            // directives: true,
            // version: '2.1.5',
          }),
          IconsResolver({
            enabledCollections: ['ep'],
          }),
        ],
      }),
      createSvgIconsPlugin({
        // 指定图标文件夹
        iconDirs: [path.resolve('src/assets/icons/svg')],
        // 指定symbolId格式
        symbolId: 'icon-[name]',
        inject: 'body-last',
      }),
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
    server: {
      host: true,
      proxy: {
        [env.VITE_API_HOST]: {
          target: 'http://119.45.113.188:9710',
          changeOrigin: true,
          rewrite: (path) => path.replace(new RegExp(`${env.VITE_API_HOST}`), ''),
          ws: true,
        },
      },
    },
  }
})
